package DAL;

import Entity.Books;
import Entity.Reads;
import IDAL.ReaderPort;
import Utils.JDBCUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.util.List;

/**
 * @Author Chen
 * @Date 2022/1/7 14:48
 * @Version 1.0
 */
public class ReaderDao implements ReaderPort {
    @Override
    public int addReader(Object...args) {
        int result = 0;
        Connection conn = null;
        try {
            QueryRunner runner = new QueryRunner();
            conn = JDBCUtils.getConnection3();
            String sql = "insert into l_reads(readesName,birthday,sex,phone,department,regdate, readesTypeNumber)values(?,?,?,?,?,?,?)";

            result = runner.update(conn, sql, args);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(conn, null);
        }
        return result;
    }

    @Override
    public int deleteReader(Object...args) {
        int result = 0;
        Connection conn = null;
        try {
            QueryRunner runner = new QueryRunner();
            conn = JDBCUtils.getConnection3();
            String sql = "delete from l_reads where readesNumber=?";
            result = runner.update(conn, sql, args);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(conn, null);
        }
        return result;
    }

    @Override
    public int updateReader(Object...args) {
        int result = 0;
        Connection conn = null;
        try {
            QueryRunner runner = new QueryRunner();
            conn = JDBCUtils.getConnection3();
            String sql = "update l_reads set " +
                    " readesName = ?," +
                    " birthday = ?," +
                    " sex = ?," +
                    " phone = ?," +
                    "`department` = ?," +
                    " regdate = ?," +
                    " readesTypeNumber = ? " +
                    " where readesNumber = ?";
            result = runner.update(conn, sql, args);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(conn, null);
        }
        return result;
    }

    @Override
    public List<Reads> selectAllReader() {
        List<Reads> result = null;
        Connection conn = null;
        try {
            QueryRunner runner = new QueryRunner();
            conn = JDBCUtils.getConnection3();
            String sql = "select * from l_reads";
            BeanListHandler<Reads> handler = new BeanListHandler<>(Reads.class);
            result = runner.query(conn, sql, handler);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(conn, null);
        }
        return result;
    }

    @Override
    public Reads selectReaderById(Object... args) {
        Reads result = null;
        Connection conn = null;
        try {
            QueryRunner runner = new QueryRunner();
            conn = JDBCUtils.getConnection3();
            String sql = "select * from l_reads where readesNumber = ?";
            BeanHandler<Reads> handler = new BeanHandler<>(Reads.class);
            result = runner.query(conn, sql, handler, args);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(conn, null);
        }
        return result;
    }

    @Override
    public List<Reads> selectReaderByName(Object... args) {
        List<Reads> result = null;
        Connection conn = null;
        try {
            QueryRunner runner = new QueryRunner();
            conn = JDBCUtils.getConnection3();
            String sql = "select * from l_reads where readesName = ?";
            BeanListHandler<Reads> handler = new BeanListHandler<>(Reads.class);
            result =  runner.query(conn, sql, handler, args);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(conn, null);
        }
        return result;
    }

    @Override
    public List<Reads> selectReaderType(Object... args) {
        List<Reads> result = null;
        Connection conn = null;
        try {
            QueryRunner runner = new QueryRunner();
            conn = JDBCUtils.getConnection3();
            String sql = "select * from l_reads where readesTypeNumber = ? ";
            BeanListHandler<Reads> handler = new BeanListHandler<>(Reads.class);
            result =  runner.query(conn, sql, handler, args);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(conn, null);
        }
        return result;
    }
}
